﻿using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EmoneyWeiXin.Controllers
{
    public class JiaQQNewController : Controller
    {
        //
        // GET: /JiaQQNew/

        //static string keyPreFix = "qunType";
        //public static Dictionary<string, string> dics = null;
        static MemcachedTools1 mmtools = new MemcachedTools1();
        static string PreFixmemkey = "jiaqunCacheKey01";
        static string loginCookieKey = "jiaqunCookieKey";

        static object lockObj = new object();
        [HttpGet]
        public ActionResult ToQQqun()
        {

            try
            {
                string typeId = Request["typeId"];
                string qqKey = mmtools.Get<string>(PreFixmemkey + typeId);
                if (string.IsNullOrEmpty(qqKey))
                {
                    LoadDataToCache();
                    qqKey = mmtools.Get<string>(PreFixmemkey + typeId);
                }
                string url = Config.JiaQQQunUrl;
                return Redirect(string.Format(url, qqKey));
            }
            catch (Exception)
            {
                return Content("跳转发生了错误");
            }

        }


        private string CurrentUser
        {
            get
            {
                    
                HttpCookie cookie = Request.Cookies[loginCookieKey];
                if (cookie != null)
                {
                    return cookie.Value;
                }
                return null;
            }
        }
        public ActionResult QQqunManage()
        {
            if (CurrentUser == null)
            {  //判断有没有登录
                return RedirectToAction("LoginPage");
            }
            return View();
        }

        [HttpPost]
        public ActionResult DoAddType()
        {
            string typeName = Request["TypeName"];
            string typePortName = Request["typePortName"];
            if (CurrentUser == null)
            {
                return Content("notlogin");
            }
            string createUser = CurrentUser;
            try
            {
                int count = QQKeysNewBLL.InsertQQKeyTypes(typeName, typePortName, createUser);
                if (count > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            catch (Exception)
            {
                return Content("0");
            }

        }


        public ActionResult LoginOut() 
        {
            Response.Cookies.Remove(loginCookieKey);
            return View();
        }

        [HttpPost]
        public ActionResult DoAddQunData()
        {
            string txtName = Request["txtName"];
            string number = Request["number"];
            string key = Request["key"].Trim();
            string typeid = Request["typeId"];
            try
            {
                if (CurrentUser == null)
                {
                    return Content("notlogin");
                }
                int count = QQKeysNewBLL.InsertQQKeysNew(txtName, number, int.Parse(typeid), key, CurrentUser);
                if (count > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        [HttpPost]
        public ActionResult GetAllTypes()
        {
            DataTable dt = QQKeysNewBLL.GetAllTypes(CurrentUser);
            string json = JsonTools.ToJson(dt); 
            return Content(json);   
        }

        public ActionResult GetAllName()
        {
            DataTable dt = QQKeysNewBLL.GetAllName(CurrentUser);
            DataRow dr = dt.NewRow();
            dr[0] = "全部";
            dt.Rows.InsertAt(dr, 0);
            string json = JsonTools.ToJson(dt);
            return Content(json);
        }

        [HttpPost]
        public ActionResult GetAllQunData()
        {
            string typeId = Request["typeId"];
            string name = Request["Name"];
            DataTable dt = QQKeysNewBLL.GetQQkeysByTypeId(typeId, name, CurrentUser);
            return Content(JsonTools.ToJson(dt));
        }

        [HttpPost]
        public ActionResult DeleteQun()
        {
            try
            {
                string id = Request["id"];
                int count = QQKeysNewBLL.DeleteQQKey(id);
                if (count > 0)
                {
                    return Content("1");
                }
                else
                {
                    return Content("0");
                }
            }
            catch (Exception)
            {
                return Content("0");
            }

        }


        public ActionResult LoginPage()
        {
            return View();
        }

        public ActionResult DoLogin()
        {
            string userName = Request["userName"];
            string passWord = Request["passWord"];
            DataTable dt = QQKeysNewBLL.checkLogin(userName, passWord);
            if (dt.Rows.Count > 0) //说明登录成功
            {
                HttpCookie newCookie = new HttpCookie(loginCookieKey, dt.Rows[0]["userName"].ToString());
                newCookie.Expires = System.DateTime.Now.AddHours(2);
                Response.Cookies.Add(newCookie);
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }

        public ActionResult DeleteType()
        {

            string typeName = Request["TypeName"];
            if (CurrentUser == null)
            {
                return Content("notlogin");
            }
            string id = Request["Id"];
            int count = QQKeysNewBLL.DeleteType(id);
            if (count > 0)
            {
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }

        public static void SetToCache(string type, string key)
        {
            mmtools.Add(PreFixmemkey + type, key, 48, 0, 0);
        }



        public ActionResult QiYong()
        {
            try
            {
                string id = Request["Id"];
                string typeId = Request["typeId"];
                string key = Request["key"].Trim();
                int count = QQKeysNewBLL.SetUsing(id, typeId);
                SetToCache(typeId, key);
                return Content("1");
            }
            catch (Exception)
            {

                return Content("0");
            }


        }

        public static void LoadDataToCache()
        {
            lock (lockObj)
            {
                DataTable dt = QQKeysNewBLL.GetUsingQQkeys();
                foreach (DataRow item in dt.Rows)
                {
                    SetToCache(item["QQTypeId"].ToString(), item["QQKey"].ToString());
                }
            }
        }
    }
}
